﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityModel.Client;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace OAuth.EFCore.Api.Controller
{
    [Route("api/[controller]")]
    [ApiController]
    public class SSMController : ControllerBase
    {
        [HttpPost("AccessToken")]
        public async Task<dynamic> AccessToken()
        {
            // 从元数据中发现端口
            var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
            if (disco.IsError)
            {
                return disco.Error;
            }
            // 请求令牌
            var tokenClient = new TokenClient(disco.TokenEndpoint, "client", "secret");
            var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1");
            if (tokenResponse.IsError)
            {
                return tokenResponse.Error;
            }
            return tokenResponse.Json;
        }
    }
}